package com.vnp.cooking;

import java.io.File;
import java.io.FileOutputStream;
import java.util.ArrayList;

import com.nostra13.universalimageloader.core.ImageLoader;
import com.vnp.cooking.UIcontrols.swipedimiss.SwipeDismissListViewTouchListener;
import com.vnp.cooking.adapter.ListCachlamAdapter;
import com.vnp.cooking.bean.ObjCachlam;
import com.vnp.cooking.bean.ObjCookpad;
import com.vnp.cooking.bean.ObjNguyenlieu;
import com.vnp.cooking.utils.Common;
import com.vnp.cooking.utils.KeyboardHelper;

import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.Bitmap.CompressFormat;
import android.graphics.Bitmap.Config;
import android.graphics.BitmapFactory;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.ImageView.ScaleType;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

public class TaoMonAnCachLamActivity extends Activity implements OnClickListener{
	
	ImageButton btnBack, btnNextStep;
	// khai bao listview
	ListView lsCachlam;
	ListCachlamAdapter adapter;
	ArrayList<ObjCachlam> objCachlams = new ArrayList<ObjCachlam>();
	
	
	// khai bao cac phan tu trong footer
	TextView txtThutubuoclam;
	int countThutu = 0;
	EditText edtMotacacbuoc;
	ImageView imgBuoclam;
	Button btnThem;
	String urlImageCachlam;
	// request code choose image
	private static final int CHOOSE_IMAGE_STEP = 1;	
	private static final String TEMP_STEP_FILE = "temporary_step_holder.jpg";
	
	// khai bao object mon an
	ObjCookpad objCookpad = new ObjCookpad();
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_tao_mon_an_cach_lam);
		
		objCookpad = getIntent().getParcelableExtra("objCookpad");
		
		//log
		Log.i("Test", "Ten mon an: "+objCookpad.getNameCookpad());
		
		btnBack = (ImageButton) findViewById(R.id.btn_back);
		btnNextStep = (ImageButton) findViewById(R.id.btn_next_step);
		lsCachlam = (ListView) findViewById(R.id.ls_cachlam);
		
		adapter = new ListCachlamAdapter(this, objCachlams);
//		addFooterListview();
		// set cho listview
		lsCachlam.setAdapter(adapter);
		
		
		// set su kien click cho cac nut
		btnBack.setOnClickListener(this);
		btnNextStep.setOnClickListener(this);
		btnThem.setOnClickListener(this);
		// set swipe nguyen lieu
		SwipeDismissListViewTouchListener touchListener =
	        new SwipeDismissListViewTouchListener(
	                lsCachlam,
	                new SwipeDismissListViewTouchListener.OnDismissCallback() {
	                    @Override
	                    public void onDismiss(ListView listView, int[] reverseSortedPositions) {
	                        for (int position : reverseSortedPositions) {
	                            adapter.remove(adapter.getItem(position));
	                        }
	                        adapter.notifyDataSetChanged();
	                    }
	                });
		
		lsCachlam.setOnTouchListener(touchListener);
		lsCachlam.setOnScrollListener(touchListener.makeScrollListener());
		imgBuoclam.setOnClickListener(this);
		
	}


	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.btn_back:
			finish();
			break;

		case R.id.btn_next_step:
			checkCachlamnextstep();
			break;
		/*case R.id.footer_btn_them_buoclam:
			addCachlam();
			break;
			
		case R.id.footer_img_tungbuoc:
			KeyboardHelper.hideSoftKeyboard(TaoMonAnCachLamActivity.this);
			callIntentPickImage();
			break;*/
		}
	}
	
	/*// add footer cach lam
	public void addFooterListview() {
		View footerView = getLayoutInflater().inflate(R.layout.footer_listview_cachlam, lsCachlam, false);
		lsCachlam.addFooterView(footerView);
		edtMotacacbuoc = (EditText) footerView.findViewById(R.id.footer_edt_mota_tungbuoc);
		txtThutubuoclam = (TextView) footerView.findViewById(R.id.footer_thu_tu_buoc_lam);
		imgBuoclam = (ImageView) footerView.findViewById(R.id.footer_img_tungbuoc);
		btnThem = (Button) footerView.findViewById(R.id.footer_btn_them_buoclam);
	}
	*/
	// check cachlamf next step
	public void checkCachlamnextstep() {
		// set cac buoc lam
		objCookpad.setObjCachlams(objCachlams);
		
		/*Intent t = new Intent(TaoMonAnCachLamActivity.this, TaoMonAnKetQuaActivity.class);
		t.putExtra("objCookpad", objCookpad);
		startActivity(t);*/
	}
	
	// add cach lam
	public void addCachlam() {
		if (edtMotacacbuoc.getText().toString().matches("")) {
			
		} else {
			countThutu = countThutu + 1;
			/*ObjCachlam objCachlam = new ObjCachlam(edtMotacacbuoc.getText().toString(), urlImageCachlam, countThutu );
			objCachlams.add(objCachlam);
			edtMotacacbuoc.setText("");
			imgBuoclam.setImageDrawable(getResources().getDrawable(R.drawable.ic_themanh));
			adapter.notifyDataSetChanged();
			txtThutubuoclam.setText("BÆ°á»›c "+(countThutu+1));*/
		}
	}
	
	// choose image intent pick
	public void callIntentPickImage() {
		Intent c = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
		c.setType("image/*");
		startActivityForResult(c, CHOOSE_IMAGE_STEP);
	}


	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);
		if (data != null && resultCode == RESULT_OK) {
			switch (requestCode) {
			case CHOOSE_IMAGE_STEP:
				onChooseImageStep(requestCode, resultCode, data);
				break;
			default:
				break;
			}
		} else {
			Toast.makeText(this, "Báº¡n chÆ°a chá»�n áº£nh cho bÆ°á»›c nÃ y", Toast.LENGTH_SHORT).show();
		}
	}
	
	// change imageview sau khi chon anh
	public void onChooseImageStep(int requestCode, int resultCode, Intent data) {
		Bitmap bm = null;
		String path = "";
		Uri u = (Uri) data.getData();
		path = getRealPathFromUri(u);
		// get bitmap
		BitmapFactory.Options options = null;
		options = new BitmapFactory.Options();
		options.inPreferredConfig = Config.RGB_565;
		options.inDither = true;
		options.inSampleSize = 2;
		options.outHeight = 100;
		options.outWidth = 40;
		bm = BitmapFactory.decodeFile(path, options);
		File f = new File(Common.SDCARD_IMAGE, TEMP_STEP_FILE);
		FileOutputStream fo;
		try {
			fo = new FileOutputStream(f);
			bm.compress(CompressFormat.JPEG, 70, fo);
		} catch (Exception e) {
			e.printStackTrace();
		}
		imgBuoclam.setScaleType(ScaleType.CENTER_CROP);
		imgBuoclam.setImageBitmap(bm);
		
		
		// doan nay la de upload anh len server se xu li o doan nay
	}
	
	// get real path
	public String getRealPathFromUri(Uri contentUri) {
		String[] filePathColumn = {MediaStore.Images.Media.DATA};
		Cursor cursor = getContentResolver().query(contentUri, filePathColumn, null, null, null);
		cursor.moveToFirst();
		int columnIndex        = cursor.getColumnIndex(filePathColumn[0]);
		final String file_path = cursor.getString(columnIndex);
		cursor.close();
		return file_path;
	}
	
	

}
